package com.fhzn.boss.web.converter;

import com.fhzn.boss.entity.App;
import com.fhzn.boss.tools.DateTimeUtils;
import com.fhzn.boss.web.vo.AppExportVO;
import com.fhzn.boss.web.vo.AppVO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/1/3
 */
public interface AppConverter {

    /**
     * 应用实体转换
     *
     * @param app 请求参数
     * @return 返回参数
     */
    static AppVO fromApp(App app) {
        AppVO appVO = new AppVO();
        if (app != null) {
            appVO.setCode(app.getCode());
            appVO.setComment(app.getComment());
            appVO.setId(app.getId());
            appVO.setName(app.getName());
            appVO.setStatus(app.getStatus());
            appVO.setCreator(app.getCreator());
            appVO.setModifier(app.getModifier());
            if (app.getCreatedTime() != null) {
                appVO.setCreatedTime(DateTimeUtils.format(app.getCreatedTime()));
            }
            if (app.getUpdatedTime() != null) {
                appVO.setUpdatedTime(DateTimeUtils.format(app.getUpdatedTime()));
            }
            return appVO;
        }
        return null;
    }

    /**
     * 应用导出实体转换
     *
     * @param app 请求参数
     * @return 返回参数
     */
    static AppExportVO fromExportApp(App app) {
        AppExportVO appVO = new AppExportVO();
        if (app != null) {
            appVO.setCode(app.getCode());
            appVO.setName(app.getName());
            appVO.setComment(app.getComment());
            return appVO;
        }
        return null;
    }

    /**
     * 应用列表转换
     *
     * @param apps 请求参数
     * @return 返回参数
     */
    static List<AppExportVO> fromAppList(List<App> apps) {
        if (CollectionUtils.isEmpty(apps)) {
            return Lists.newArrayList();
        }
        return apps.stream().map(AppConverter::fromExportApp).collect(Collectors.toList());
    }
}
